package slog

import (
	"autoTestNew/pkg/dashboard/autoTest/util"
	"fmt"
	"log"
	"os"

	"go.uber.org/zap"
	"go.uber.org/zap/zapcore"
	"gopkg.in/natefinch/lumberjack.v2"
)

var Flog *zap.Logger
var Slog *zap.SugaredLogger

func init() {
	// 生成日志文件名
	suffix := util.GetNowDateTimeString()
	// 将日志文件保存到根目录
	logFileName := fmt.Sprintf("/%s.log", suffix)
	//文件writeSyncer
	fileWriteSyncer := zapcore.AddSync(&lumberjack.Logger{
		Filename: logFileName, //日志文件存放目录
	})
	//第三个及之后的参数为写入文件的日志级别,ErrorLevel模式只记录error级别的日志
	fileCore := zapcore.NewCore(
		zapcore.NewConsoleEncoder(zap.NewDevelopmentEncoderConfig()),
		zapcore.NewMultiWriteSyncer(fileWriteSyncer, zapcore.AddSync(os.Stdout)),
		zapcore.DebugLevel,
	)
	Flog = zap.New(fileCore, zap.AddCaller())
	Slog = Flog.Sugar()
	Slog.Infof("初始化日志 zap 成功")
}

func Sync() {
	err := Flog.Sync()
	if err != nil {
		log.Fatalf("刷新日志 zap 失败: %s", err.Error())
	}
}
